import { getUserInfo } from '@/api/user'
import {
  setStorageToken,
  removeStorageToken,
  getStorageToken
} from '@/utils/storage'
const state = {
  // 保存登录成功的token信息
  token: getStorageToken(),
  userInfo: {}
}

const mutations = {
  setToken(state, payload) {
    state.token = payload
    setStorageToken(payload)
  },
  removeToken(state) {
    state.token = {}
    removeStorageToken()
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  }
}

const actions = {
  async Actions_getUserInfo(context) {
    try {
      const res = await getUserInfo()
      context.commit('setUserInfo', res.data)
    } catch {
      // 获取用户信息失败
      // 提示下
      this.$toast('获取用户信息失败')
    }
  }
}
const getters = {}

export default {
  state,
  mutations,
  getters,
  actions,
  // 开启命名空间
  namespaced: true
}
